﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ShareSceneScript : MonoBehaviour {

	TextMesh highScoreText;
	
	Animator fbAnim;
	SpriteRenderer fbRend;
	Animator twitterAnim;
	SpriteRenderer twitterRend;
	Animator linkedInAnim;
	SpriteRenderer linkedInRend;
	Animator gplusAnim;
	SpriteRenderer gplusRend;
	Animator pintAnim;
	SpriteRenderer pintRend;
	Animator emailAnim;
	SpriteRenderer emailRend;
	Animator smsAnim;
	SpriteRenderer smsRend;
	Animator backAnim;
	SpriteRenderer backRend;

	SpriteRenderer slideRender;
	SpriteRenderer barRender;
	
	GameObject mainMenu;
	
	List<SpriteRenderer> scrollRenderers = new List<SpriteRenderer>();
	List<TextMesh> textMeshes = new List<TextMesh>();
	
	SpriteRenderer scrollMe1;
	TextMesh scrollMe2;
	
	float flickerStart = 2f;
	float flickerCount = 2f;
	bool flickState = true;

	string shareLinkString = "https://www.johnrussellernest.com/CMS/herobird";
	
	// Use this for initialization
	void Start () {
		highScoreText = (TextMesh)GameObject.Find ("HighScoreText").GetComponent (typeof(TextMesh));
		GameMaintainerSingleton.Instance.GetHighScore ();
		string diff = "";
		switch(GameMaintainerSingleton.Instance.Difficulty) {
		case 0:diff = "Easy";break;
		case 1:diff = "Normal";break;
		case 2:diff = "Hard";break;
		default:break;
		}
		highScoreText.text = "High Score ("+diff+"): " + GameMaintainerSingleton.Instance.HighScore;
		
		this.fbAnim = (Animator)GameObject.Find ("Facebook").GetComponent (typeof(Animator));
		this.fbRend = (SpriteRenderer)GameObject.Find("Facebook").GetComponent (typeof(SpriteRenderer));
		this.twitterAnim = (Animator)GameObject.Find ("Twitter").GetComponent (typeof(Animator));
		this.twitterRend = (SpriteRenderer)GameObject.Find("Twitter").GetComponent (typeof(SpriteRenderer));
		this.linkedInAnim = (Animator)GameObject.Find ("LinkedIn").GetComponent (typeof(Animator));
		this.linkedInRend = (SpriteRenderer)GameObject.Find ("LinkedIn").GetComponent (typeof(SpriteRenderer));
		this.gplusAnim = (Animator)GameObject.Find ("GooglePlus").GetComponent (typeof(Animator));
		this.gplusRend = (SpriteRenderer)GameObject.Find ("GooglePlus").GetComponent (typeof(SpriteRenderer));
		this.pintAnim = (Animator)GameObject.Find ("Pinterest").GetComponent (typeof(Animator));
		this.pintRend = (SpriteRenderer)GameObject.Find ("Pinterest").GetComponent (typeof(SpriteRenderer));
		this.emailAnim = (Animator)GameObject.Find ("Email").GetComponent (typeof(Animator));
		this.emailRend = (SpriteRenderer)GameObject.Find ("Email").GetComponent (typeof(SpriteRenderer));
		this.smsAnim = (Animator)GameObject.Find ("SMS").GetComponent (typeof(Animator));
		this.smsRend = (SpriteRenderer)GameObject.Find ("SMS").GetComponent (typeof(SpriteRenderer));
		this.backAnim = (Animator)GameObject.Find ("Back").GetComponent (typeof(Animator));
		this.backRend = (SpriteRenderer)GameObject.Find ("Back").GetComponent (typeof(SpriteRenderer));

		
		this.slideRender = (SpriteRenderer)GameObject.Find("ScrollMarker").GetComponent(typeof(SpriteRenderer));
		this.barRender = (SpriteRenderer)GameObject.Find("ScrollBar").GetComponent(typeof(SpriteRenderer));
		
		this.mainMenu = (GameObject)GameObject.Find("MainMenu");
		
		this.fbAnim.SetInteger (MenuButtonConstants.MenuTriggerString, (int)MenuButtonConstants.MenuConstants.FacebookUp);
		this.twitterAnim.SetInteger (MenuButtonConstants.MenuTriggerString, (int)MenuButtonConstants.MenuConstants.TwitterUp);
		this.linkedInAnim.SetInteger (MenuButtonConstants.MenuTriggerString, (int)MenuButtonConstants.MenuConstants.LinkedinUp);
		this.gplusAnim.SetInteger (MenuButtonConstants.MenuTriggerString, (int)MenuButtonConstants.MenuConstants.GoogleplusUp);
		this.pintAnim.SetInteger (MenuButtonConstants.MenuTriggerString, (int)MenuButtonConstants.MenuConstants.PinterestUp);
		this.emailAnim.SetInteger(MenuButtonConstants.MenuTriggerString, (int)MenuButtonConstants.MenuConstants.EmailUp);
		this.smsAnim.SetInteger(MenuButtonConstants.MenuTriggerString, (int)MenuButtonConstants.MenuConstants.SMSUp);
		this.backAnim.SetInteger(MenuButtonConstants.MenuTriggerString, (int)MenuButtonConstants.MenuConstants.BackUp);

		int children = this.mainMenu.transform.childCount;
		for (int z=0;z<children;z++) {
			Transform child = this.mainMenu.transform.GetChild(z);
			if (child.name != "ScrollMe" && child.name != "ScrollText") {
				SpriteRenderer rend = (SpriteRenderer)child.gameObject.GetComponent(typeof(SpriteRenderer));
				TextMesh textMesh = (TextMesh)child.GetChild(0).gameObject.GetComponent(typeof(TextMesh));
				this.scrollRenderers.Add(rend);
				this.textMeshes.Add(textMesh);
			}
		}
		
		this.scrollMe1 = (SpriteRenderer)GameObject.Find("ScrollMe").GetComponent(typeof(SpriteRenderer));
		this.scrollMe2 = (TextMesh)GameObject.Find("ScrollText").GetComponent(typeof(TextMesh));
		
		this.UpdateBarFade();
		
		GameMaintainerSingleton.Instance.ShowAds ();
	}
	
	void UpdateBarFade() {
		Vector3 minBounds = Camera.main.WorldToScreenPoint(this.barRender.bounds.min);
		Vector3 maxBounds = Camera.main.WorldToScreenPoint(this.barRender.bounds.max);
		
		
		float barMin = minBounds.x + 100f;
		float barMax = maxBounds.x - 100f;


		for (int z=0;z<scrollRenderers.Count;z++) {
			SpriteRenderer rend = this.scrollRenderers[z];
			TextMesh rendText = this.textMeshes[z];
			Vector3 rendMin = Camera.main.WorldToScreenPoint(rend.bounds.min);
			Vector3 rendMax = Camera.main.WorldToScreenPoint(rend.bounds.max);
			Color col = rend.color;
			Vector3 scaleRend = rend.transform.localScale;

			if (rendMin.x > barMax) {
				float a = Mathf.Lerp(1f,0f,(rendMin.x - barMax) / ((float)Screen.width - barMax));
				float sc = Mathf.Lerp(6f,12f,(rendMin.x - barMax) / ((float)Screen.width - barMax));
				col.a = a;
				scaleRend.x = scaleRend.y = sc;
			}
			else if (rendMax.x < barMin) {
				float a = Mathf.Lerp(0f,1f,rendMax.x / barMin);
				float sc = Mathf.Lerp(12f,6f,rendMax.x / barMin);
				col.a = a;
				scaleRend.x = scaleRend.y = sc;
			}
			else {
				float a = 1f;
				float sc = 6f;
				col.a = a;
				scaleRend.x = scaleRend.y = sc;
			}
			//Debug.Log (child.name + "/" + barMin + "/" + barMax + "/" + col.ToString());
			rendText.color = col;
			rend.color = col;
			rend.transform.localScale = scaleRend;
		}
		
	}
	
	void UpdateScrollText() {
		flickerCount -= Time.deltaTime;
		if (flickerCount < 0f) {
			flickerCount = flickerStart;
			flickState = !flickState;
			Color col = this.scrollMe1.color;
			if (flickState) {
				col.a = 1f;
			}
			else {
				col.a = 0.25f;
			}
			this.scrollMe1.color = col;
			this.scrollMe2.color = col;
		}
	}
	
	// Update is called once per frame
	void Update () {
		if (Input.GetKey(KeyCode.Escape)) {
			Application.LoadLevel(0);
		}

		//Vector2 pos = Vector2.zero;
		this.UpdateScrollText();
		
		bool inFacebook = false; bool inFacebookInitially = false;
		bool inTwitter = false; bool inTwitterInitially = false;
		bool inLinkedIn = false; bool inLinkedInInitially = false;
		bool inGooglePlus = false; bool inGooglePlusInitially = false;
		bool inPinterest = false; bool inPinterestInitially = false;
		bool inEmail = false; bool inEmailInitially = false;
		bool inSMS = false; bool inSMSInitially = false;
		bool inBack = false; bool inBackInitially = false;

		bool inScrollBar = false;
		bool inScroll = false;
		
		if (GameMaintainerSingleton.Instance.CheckInTouchInteractionState()) {
			inFacebook = GameMaintainerSingleton.Instance.IsInSpriteBounds(this.fbRend, GameMaintainerSingleton.Instance.Pos);
			inTwitter = GameMaintainerSingleton.Instance.IsInSpriteBounds(this.twitterRend, GameMaintainerSingleton.Instance.Pos);
			inLinkedIn = GameMaintainerSingleton.Instance.IsInSpriteBounds(this.linkedInRend, GameMaintainerSingleton.Instance.Pos);
			inGooglePlus = GameMaintainerSingleton.Instance.IsInSpriteBounds(this.gplusRend, GameMaintainerSingleton.Instance.Pos);
			inPinterest = GameMaintainerSingleton.Instance.IsInSpriteBounds(this.pintRend, GameMaintainerSingleton.Instance.Pos);
			inEmail = GameMaintainerSingleton.Instance.IsInSpriteBounds(this.emailRend, GameMaintainerSingleton.Instance.Pos);
			inSMS = GameMaintainerSingleton.Instance.IsInSpriteBounds(this.smsRend, GameMaintainerSingleton.Instance.Pos);
			inBack = GameMaintainerSingleton.Instance.IsInSpriteBounds(this.backRend, GameMaintainerSingleton.Instance.Pos);

			inFacebookInitially = GameMaintainerSingleton.Instance.IsInSpriteBounds(this.fbRend, GameMaintainerSingleton.Instance.ClickDownPos);
			inTwitterInitially = GameMaintainerSingleton.Instance.IsInSpriteBounds(this.twitterRend, GameMaintainerSingleton.Instance.ClickDownPos);
			inLinkedInInitially = GameMaintainerSingleton.Instance.IsInSpriteBounds(this.linkedInRend, GameMaintainerSingleton.Instance.ClickDownPos);
			inGooglePlusInitially = GameMaintainerSingleton.Instance.IsInSpriteBounds(this.gplusRend, GameMaintainerSingleton.Instance.ClickDownPos);
			inPinterestInitially = GameMaintainerSingleton.Instance.IsInSpriteBounds(this.pintRend, GameMaintainerSingleton.Instance.ClickDownPos);
			inEmailInitially = GameMaintainerSingleton.Instance.IsInSpriteBounds(this.emailRend, GameMaintainerSingleton.Instance.ClickDownPos);
			inSMSInitially = GameMaintainerSingleton.Instance.IsInSpriteBounds(this.smsRend, GameMaintainerSingleton.Instance.ClickDownPos);
			inBackInitially = GameMaintainerSingleton.Instance.IsInSpriteBounds(this.backRend, GameMaintainerSingleton.Instance.ClickDownPos);

			inScroll = GameMaintainerSingleton.Instance.IsInSpriteY(this.slideRender, GameMaintainerSingleton.Instance.ClickDownPos,10);
			inScrollBar = GameMaintainerSingleton.Instance.IsInSpriteY(this.barRender, GameMaintainerSingleton.Instance.Pos,10);
			
			GameMaintainerSingleton.SpriteBoundsSplitViewModel vm = new GameMaintainerSingleton.SpriteBoundsSplitViewModel();
			if (inScroll) {
				vm = GameMaintainerSingleton.Instance.CheckBarNoSplit(
					inScroll,inScrollBar,slideRender,barRender,
					GameMaintainerSingleton.Instance.Pos, GameMaintainerSingleton.Instance.ClickDownPos, 40); 
				
				//Min 4.75
				//Max -17.41418
				Vector3 mainMenuPos = this.mainMenu.transform.position;
				mainMenuPos.x = Mathf.Lerp(4.64f,-14.771f,vm.RelativeDistance);
				this.mainMenu.transform.position = mainMenuPos;
				
				this.UpdateBarFade();
			}
		}
		
		// Facebook Button
		if (GameMaintainerSingleton.Instance.CheckButton(
			inFacebook,inFacebookInitially,
			fbRend,fbAnim,
			MenuButtonConstants.MenuConstants.FacebookDown,MenuButtonConstants.MenuConstants.FacebookUp,
			MenuButtonConstants.MenuTriggerString)) {
			Application.OpenURL("https://www.facebook.com/sharer/sharer.php?u=" + this.shareLinkString);
		}
		// Twitter Button
		else if (GameMaintainerSingleton.Instance.CheckButton(
			inTwitter,inTwitterInitially,
			twitterRend,twitterAnim,
			MenuButtonConstants.MenuConstants.TwitterDown,MenuButtonConstants.MenuConstants.TwitterUp,
			MenuButtonConstants.MenuTriggerString)) {
			Application.OpenURL("https://twitter.com/home?status=" + this.shareLinkString);
		}
		// LinkedIn Button
		else if (GameMaintainerSingleton.Instance.CheckButton(
			inLinkedIn,inLinkedInInitially,
			linkedInRend,linkedInAnim,
			MenuButtonConstants.MenuConstants.LinkedinDown,MenuButtonConstants.MenuConstants.LinkedinUp,
			MenuButtonConstants.MenuTriggerString)) {
			Application.OpenURL("https://www.linkedin.com/shareArticle?mini=true&url="+this.shareLinkString+"&title=HeroBird&summary=A%20fun%20and%20addicting%20retro%20console%20style%20science%20fiction%20game.%20Play%20it%20now!&source=");
		}
		// Google+ Button
		else if (GameMaintainerSingleton.Instance.CheckButton(
			inGooglePlus,inGooglePlusInitially,
			gplusRend,gplusAnim,
			MenuButtonConstants.MenuConstants.GoogleplusDown,MenuButtonConstants.MenuConstants.GoogleplusUp,
			MenuButtonConstants.MenuTriggerString)) {
			Application.OpenURL("https://plus.google.com/share?url="+this.shareLinkString);
		}
		// Pinterest Button
		else if (GameMaintainerSingleton.Instance.CheckButton(
			inPinterest, inPinterestInitially, 
			pintRend, pintAnim, 
			MenuButtonConstants.MenuConstants.PinterestDown, MenuButtonConstants.MenuConstants.PinterestUp,
			MenuButtonConstants.MenuTriggerString)) {
			Application.OpenURL("https://pinterest.com/pin/create/button/?url=HeroBird&media=https://www.johnrussellernest.com/CMS/Media/Default/Homepage/HB_SCR_GAME.png&description=Check%20out%20HeroBird%20at%20"+this.shareLinkString);
			
		}
		// Email Button
		else if (GameMaintainerSingleton.Instance.CheckButton(
			inEmail, inEmailInitially, 
			emailRend, emailAnim, 
			MenuButtonConstants.MenuConstants.EmailDown, MenuButtonConstants.MenuConstants.EmailUp,
			MenuButtonConstants.MenuTriggerString)) {
			Application.OpenURL("mailto:?subject=HeroBird&body="+this.shareLinkString);
		}
		// SMS Button
		else if (GameMaintainerSingleton.Instance.CheckButton(
			inSMS, inSMSInitially, 
			smsRend, smsAnim, 
			MenuButtonConstants.MenuConstants.SMSDown, MenuButtonConstants.MenuConstants.SMSUp,
			MenuButtonConstants.MenuTriggerString)) {
			Application.OpenURL("sms:?body="+this.shareLinkString);
		}
		// Back Button
		else if (GameMaintainerSingleton.Instance.CheckButton(
			inBack, inBackInitially, 
			backRend, backAnim, 
			MenuButtonConstants.MenuConstants.BackDown, MenuButtonConstants.MenuConstants.BackUp,
			MenuButtonConstants.MenuTriggerString)) {
			Application.LoadLevel(0);
		}
	}
}
